// src/main/java/csu/web/credit_bank/service/EmailService.java

package csu.web.credit_bank.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Async // 【关键】将此方法标记为异步方法，它将在独立的线程中执行
    public void sendSimpleMail(String to, String subject, String text, String from) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        message.setFrom(from);

        try {
            mailSender.send(message);
        } catch (Exception e) {
            // 在实际项目中，这里应该添加更详细的日志记录
            System.err.println("发送邮件时发生错误: " + e.getMessage());
        }
    }
}